package com.ttc.mBackup.BackupAgent;


import java.io.File;
import java.util.ArrayList;

import com.ttc.mBackup.BackupAgent.ListLocaltion;
import com.ttc.mBackup.BackupAgent.listviewlocal;
import com.ttc.mBackup.BackupActivity;
import com.ttc.mBackup.R;
import com.ttc.mBackup.BackupAgent.LoginDropbox;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BARLocaltion extends Activity {

	
	public final static String DIR_CHOICE="DIRECTORY";
	public final static String CLOUD_STORE ="DROPBOX";
	//public final static String KEY_DIR="DIR";
	ArrayList<listviewlocal> model = new ArrayList<listviewlocal>();
	ListLocaltion adapter;
public	Bundle extras ;
private ListView lv;
private ImageView up;
private SharedPreferences pref ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.barlocal);
		/*Widget*/
		TextView txt = (TextView) findViewById(R.id.title_local);
		TextView tv = (TextView) findViewById(R.id.detail_local);
		up = (ImageView) findViewById(R.id.image_up);
		up.setVisibility(8);
		/*Data Receive*/
		Bundle extra = getIntent().getExtras();
		
		pref= getSharedPreferences(
				LoginDropbox.ACCOUNT_PREFS_NAME, MODE_WORLD_WRITEABLE);
	//Create a Extra to send for PerformTask Via BARSelector() Activity
		extras= new Bundle();
		if(extra!=null)
		{
			/*check what task whe want to perform*/
			if(extra.getBoolean(BackupActivity.KEY_ACTIVITY))
			{
				txt.setText("BACKUP LOCATION");
				tv.setText("Chose where you can put your backup files to backup ");
				extras.putBoolean(BackupActivity.KEY_ACTIVITY, true);
				
			}
			else
			{
				txt.setText("RESTORE LOCATION");
				tv.setText("Chose where you can get your backup files to restore ");
				extras.putBoolean(BackupActivity.KEY_ACTIVITY, false);
			}
		}
	
		/*
		 * Set Adapter for ListView 
		 * */
lv = (ListView) findViewById(R.id.list_local);
  
  /*Device ITEM*/
  model.add(new listviewlocal("Devices",
							R.drawable.devicesicon,
							"Place your backup files into Internal Storage"));
  /*SDCard Item*/
  model.add(new listviewlocal("SD Card",
							R.drawable.sdicon,
							"Place your backup files into External Storage"));
  /*Clound Item*/
  model.add(new listviewlocal("DropBox",
							R.drawable.dropbodx,
							pref.getBoolean(LoginDropbox.LOG_DROPBOX, false)?" ":"You's not configurate for Dropbox")
					);
 
 
	lv.setAdapter(new ListLocaltion(model));
	/*
	 * Set choice mode for listview, choice one item
	 * */
	lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	
	/*Set on Item Click Listener*/
	lv.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
			//Choose Position Item to perform your command
			Intent i = new Intent("com.ttc.mBackup.BackupAgent.BARADDTASK");
			
			switch(arg2)
			{
			case 0: 
				
			/*
			 * Director on device 
			 * */
				
				extras.putCharSequence(DIR_CHOICE, "data/data/com.ttc.mBackup/SystemBackup");
				//extras.putBoolean(CLOUD_STORE, false);
				/*
				 * Call BARSelector() Activity
				 * */
				i.putExtras(extras);
				startActivity(i);
				break;
			case 1: 
				/*
				 * Director on SDCard 
				 * */
				
				extras.putCharSequence(DIR_CHOICE, new File(Environment.getExternalStorageDirectory().getAbsolutePath()
		        		+File.separator+
		        		BackupActivity.DIR_NAME).getAbsolutePath());
				/*
				 * Call BARSelector() Activity
				 * */
				//extras.putBoolean(CLOUD_STORE, false);
				i.putExtras(extras);
				startActivity(i);
				//Toast.makeText(getBaseContext(), Environment.getExternalStorageDirectory().getAbsolutePath(), Toast.LENGTH_SHORT).show();
				break;
			case 2:
				
				/*If is logined - call BARLocation or not Call DropBoxMain*/
				if(pref.getBoolean(LoginDropbox.LOG_DROPBOX, false))
				{
				//	Toast.makeText(getBaseContext(),pref.getString(LoginDropbox.ACCOUNT_DROPBOX,
						//	""), Toast.LENGTH_SHORT).show();
					
					extras.putCharSequence(DIR_CHOICE,"/BackupSystems");
					extras.putBoolean(CLOUD_STORE, true);
					Intent i1 = new Intent("com.ttc.mBackup.BackupAgent.LOGINADDTASK");
					i1.putExtras(extras);
					startActivity(i1);
				}
				else
				{
			
				startActivity(new Intent("com.ttc.mBackup.BackupAgent.DROPBOXMAIN"));
				
					}
				
				break;
			}
			
		}
		
	
	});
		
	}

	


	
}
